<template lang="pug">
  .page
    .AccountsReceivableManage
      .search-form.mb-10.pl-20.pr-10
        Form.ivu-row.ivu-form-basic(ref="formData" :model="formData" :rules="rules" :label-width="70")
          FormItem.form-item.ivu-col.pr-20.mb-15(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop")
            template(v-if="item.type === 'select'")
              Select(v-model="formData[item.model]" :placeholder="item.placeholder" clearable filterable)
                Option(
                  v-for="(c_item, index) in arrListDown[item.options]"
                  :value="c_item.label"
                  :key="index"
                ) {{ c_item.label }}
            template(v-if="item.type === 'input'")
              Input(v-model="formData[item.model]" :placeholder="item.placeholder" clearable)
            template(v-if="item.type === 'button'")
              Button(type="info" @click="searchFn") 查询

      .pl-20.pr-20.bg-white
        Table.hasTitle-table.beautiful-table(:columns="thead" :data="tableData" :stripe="true" ref="demoTable" @on-selection-change="handleOnSelectChange")
          .clearfix.font-16(slot="header")
            .table-title-lefttxt.pull-left
              |合计
              span.text-info.pl-5.pr-5.font-weight {{tableComone.totalRows}}
              |条
            .pull-right.table-title-btns
              Button.mr-10(type="info" @click="handleShowCreateEnterpriseCertificateModal(null)") 新增
              Button.mr-10(type="info" @click="handleShowBatchImportModal") 导入
              Button.mr-10(type="warning" @click="handleExport") 导出
          .clearfix.pl-20.pr-20(slot="footer")
            Page(
              :total="tableComone.totalRows"
              @on-change="onChangePage"
              @on-page-size-change="onpageSizeChange"
              show-sizer
              show-total
              placement="top"
              prev-text="上一页"
              next-text="下一页"
              :page-size-opts="[10, 20, 30, 50, 100]"
            )
          template(slot-scope="{ row, index }" slot="fileName")
            span.mr-10 {{row.fileName}}
            Icon.cursor-pointer.font-16.text-primary.mr-10(v-if="row.filePath && ['jpg', 'png', 'gif'].indexOf(row.filePath.substring(row.filePath.lastIndexOf('.') + 1) + '') !== -1" type="md-eye" size="small" @click="previewImg(row.filePath)") 预览
            Icon.cursor-pointer.font-16.text-primary(v-if="row.filePath" type="md-download" size="small" @click="accessoryDownload(row.filePath)") 下载
          template(slot-scope="{ row, index }" slot="action")
            Button(type="text" size="small" @click="handleShowCreateEnterpriseCertificateModal(row.id)") 编辑
            Button(type="text" size="small" @click="isDeleteModal(row.id)") 删除

    //- 新建弹窗
    CreateEnterpriseCertificateModal(ref="CreateEnterpriseCertificateModal" @query="searchFn")

    //-图片预览
    Modal(title="附件预览" v-model="isPreviewImgModal" footer-hide)
      img(:src="currentPreviewImg")
    
    //- 批量导入弹窗
    BatchImportModal(ref="BatchImportModal" @query="searchFn")
</template>

<script>
import { Common } from '@/utils/common.js'
import { BusinessDocumentApi } from '@/api/BusinessDocument.api.js'
import { EnterpriseCertificateThead } from './tableHead.js'
import _cloneDeep from 'clone-deep'
import CreateEnterpriseCertificateModal from './modal/CreateEnterpriseCertificateModal.vue'
import BatchImportModal from './modal/BatchImportModal.vue'
import { mapActions } from 'vuex'

export default {
  name: '', // 企业证件管理

  components: {
    CreateEnterpriseCertificateModal,
    BatchImportModal
  },

  data () {
    return {
      companyId: '', // 公司ID
      companyName: '', // 公司名称
      employeeId: '', // 员工ID
      thead: _cloneDeep(EnterpriseCertificateThead),
      tableData: [],
      selectedId: [], // 勾选的id集合,array格式
      selectedArr: [], // 勾选的id, name数组集合
      tableComone: {
        pageIndex: 1,
        totalRows: 0,
        pageSize: 10
      },
      formData: {
        name: ''
      },
      formItem: [
        { type: 'input', label: '证件名称：', placeholder: '证件名称', prop: 'name', model: 'name', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-9 ivu-col-span-xxl-6', options: 'tempOps' },
        { type: 'button', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-3 ivu-col-span-xxl-3' }
      ],
      rules: {},
      arrListDown: {
        tempOps: []
      },
      importFile: null,
      isPreviewImgModal: false,
      currentPreviewImg: null
    }
  },
  computed: {
  },

  mounted () {
    this.query()
  },

  methods: {
    ...mapActions([
      'handleSetECMWarnCount'
    ]),

    rowClassName (row, index) {
      if (row.isWarn > 0) {
        return 'text-error';
      } else {
        return
      }
    },

    // 搜索
    searchFn () {
      this.tableComone.pageIndex = 1
      this.query()
    },

    /**
     * 分页页码变动
     * @param {Number} val pageIndex
     */
    onChangePage (val) {
      this.tableComone.pageIndex = val
      this.query()
    },
    /**
     * 分页页码变动
     * @param {Number} val pageSize
     */
    onpageSizeChange (val) {
      this.tableComone.pageIndex = 1
      this.tableComone.pageSize = val
      this.query()
    },
    /**
     * 分页查询接口
     * @param {Object} obj {pageIndex, pageSize}
     */
    async query () {
      var params = {
        HasPaging: true,
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize
      }
      let res = await BusinessDocumentApi.getList({ ...params, ...this.formData })
      console.log('获取列表res: ', res)
      if (res.status === 200 && res.data.success) {
        this.tableData = res.data.data
        this.tableComone.totalRows = res.data.totalRows
        this.handleSetECMWarnCount(res.data.warnCount)
      } else {
        this.tableData = []
      }
      this.loading = false
    },

    // 表格选择操作
    handleOnSelectChange (value) {
      this.selectedArr = value.map((item) => {
        return { id: item.id, name: item.invoiceItem }
      })
      this.selectedId = value.map((item) => {
        return item.id
      })
      console.log('列表勾选数组1', this.selectedArr)
      console.log('列表勾选数组2', this.selectedId)
    },

    // 显示新增弹窗
    handleShowCreateEnterpriseCertificateModal (id) {
      this.$refs.CreateEnterpriseCertificateModal.show()
      this.$refs.CreateEnterpriseCertificateModal.createEnterpriseCertificateModalTitle = id ? '修改企业证件' : '新增企业证件'
      // this.$refs.CreateEnterpriseCertificateModal.getModel(id)
      if (id) {
        this.$refs.CreateEnterpriseCertificateModal.getModel(id)
      }
    },

    // 是否删除前询问
    isDeleteModal (id) {
      this.$Modal.confirm({
        title: '是否确认删除',
        onOk: () => {
          this.handleSureDelete(id)
        }
      })
    },

    // 确认删除
    async handleSureDelete (id) {
      let res = await BusinessDocumentApi.deleteDocument({ id: id })
      console.log('确认删除: ', res)
      if (res.status === 200 && res.data.success) {
        this.$Message.success(res.data.message)
        this.query()
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    // 导出
    async handleExport () {
      console.log('导出 formData: ', this.formData)
      let res = await BusinessDocumentApi.export(this.formData)
      console.log('res: ', res)
      if (res.status === 200 && res.data.success) {
        console.log('导出应收账单列表 返回链接: ', res.data.message)
        window.open(baseUrl + res.data.message, '_blank')
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    // 普通附件下载
    accessoryDownload (url) {
      console.log('下载url: ', url)
      window.open(url, '_blank')
    },

    // 图片预览
    previewImg (url) {
      this.isPreviewImgModal = true
      this.currentPreviewImg = url
    },

    // 批量导入-显示弹窗
    handleShowBatchImportModal () {
      this.$refs.BatchImportModal.show()
    },
  } // methods end
}
</script>

<style lang="less" scoped>

</style>
